package com.jk.kdd.base;

import org.smart4j.framework.util.CastUtil;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by hanping.fu.
 *
 * @project official
 * @data 2016/11/28
 * @desc
 */
public class BaseController {
    // 成功
    public static final Integer SUCCESS_CODE = 200;
    // 异常
    public static final Integer ERROR_CODE = 500;
    // 验证错误。
    public static final Integer CHECK_ERROR_CODE = 422;
    // 用户没有权限
    public static final Integer UNAUTHORIZED_CODE = 401;
    // 访问被禁止（有权限，但无法操作）
    public static final Integer FORBIDDEN_CODE = 403;
    // 用户发出的请求针对的是不存在的记录
    public static final Integer NOT_FOUND_CODE = 404;
    // 请求的格式不正确（比如用户请求JSON格式，但是只有XML格式）
    public static final Integer NOT_ACCEPTABLE_CODE = 406;


    /**
     * 取得操作人ID
     * @return
     */
    public String getOperatorId(){
        return CastUtil.castString(getSessionValue("operateId"));
    }


    /**
     * 取得Request param值
     * @param paramName
     * @return
     */
    public String getRequestStringValue(String paramName){
        return CastUtil.castString(getRequestValue(paramName));
    }

    /**
     * 取得Request param值
     * @param paramName
     * @return
     */
    public Long getRequestLongValue(String paramName){
        return CastUtil.castLong(getRequestValue(paramName));
    }


    /**
     * 取得Request param值
     * @param paramName
     * @return
     */
    public Object getRequestValue(String paramName){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request.getParameter(paramName);
//        if(request.getMethod().toLowerCase().equals("post")){
//            Map<String,String> param = (Map<String, String>) request.getAttribute("postData");
//            if(param == null){
//                param = RequestParamUtil.getRequestParams(request);
//                request.setAttribute("postData",param);
//            }
//            return param.get(paramName);
//        }else{
//            return request.getParameter(paramName);
//        }

    }

    /**
     * 取得Session attribute String值
     * @param attributeName
     * @return
     */
    public String getSessionStringValue(String attributeName){
        return CastUtil.castString(getSessionValue(attributeName));
    }

    /**
     * 取得Session attribute Bool值
     * @param attributeName
     * @return
     */
    public boolean getSessionBoolValue(String attributeName){
        return CastUtil.castBoolean(getSessionValue(attributeName));
    }
    
    /**
     * 取得Session attribute值
     * @param attributeName
     * @return
     */
    public Object getSessionValue(String attributeName){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        return session.getAttribute(attributeName);
    }

    /**
     * 向session设置基本参数
     */
    public void setSessionValue(String key,Object value) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        session.setAttribute(key,value);
    }

    /**
     * 根目录地址
     * @return
     */
    public String getRootPath(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request.getSession().getServletContext().getRealPath("/");
    }
}
